package com.biu.billiards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biu.billiards.mapper.UserMapper;
import com.biu.billiards.model.bo.OpenDoorBO;
import com.biu.billiards.model.bo.WithdrawBO;
import com.biu.billiards.model.entity.OpenDoor;
import com.biu.billiards.model.entity.User;
import com.biu.billiards.model.entity.Withdraw;
import com.biu.billiards.mapper.WithdrawMapper;
import com.biu.billiards.model.vo.withdraw.AddWithdrawVo;
import com.biu.billiards.model.vo.withdraw.GetWithdrawVo;
import com.biu.billiards.model.vo.withdraw.UpdateWithdrawVO;
import com.biu.billiards.service.IWithdrawService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xue
 * @since 2023-11-21
 */
@Service
public class WithdrawServiceImpl extends ServiceImpl<WithdrawMapper, Withdraw> implements IWithdrawService {

    @Resource
    private WithdrawMapper withdrawMapper;


    @Override
    public IPage<WithdrawBO> getWithdrawPage(GetWithdrawVo getWithdrawVo) {
        QueryWrapper<Withdraw> query = new QueryWrapper<>();
        query.orderByDesc("withdraw_start_time");


        //新建分页条件
        Page<Withdraw> p = new Page<>(getWithdrawVo.getCurrentPage(),getWithdrawVo.getPageSize());
        IPage<Withdraw> userIPage = withdrawMapper.selectPage(p,query);
        IPage<WithdrawBO> page = new Page<>();
        List<WithdrawBO> adminDetailBOList = new ArrayList<>();
        for (Withdraw admin: userIPage.getRecords()){
            WithdrawBO adminDetailBO = new WithdrawBO();
            BeanUtils.copyProperties(admin,adminDetailBO);
            adminDetailBOList.add(adminDetailBO);
        }
        /* 将转化的BO放到的Records里面  */
        page.setRecords(adminDetailBOList);
        page.setCurrent(userIPage.getCurrent());
        page.setPages(userIPage.getPages());
        page.setPages(userIPage.getSize());
        page.setTotal(userIPage.getTotal());

        return page;

    }

    @Override
    public Boolean updateWithdraw(UpdateWithdrawVO updateWithdrawVO) {
        QueryWrapper<Withdraw> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("withdraw_id",updateWithdrawVO.getWithdrawId());

        Withdraw withdraw = withdrawMapper.selectOne(queryWrapper);

        BeanUtils.copyProperties(updateWithdrawVO,withdraw);


        return withdrawMapper.update(withdraw, queryWrapper) == 1;
    }

    @Override
    public Boolean addWithdraw(AddWithdrawVo addWithdrawVo) {
        Withdraw withdraw = new Withdraw();
        BeanUtils.copyProperties(addWithdrawVo,withdraw);

        return withdrawMapper.insert(withdraw) == 1;
    }

    @Override
    public Boolean deleteWithdraw(Integer withdrawId) {
        QueryWrapper<Withdraw> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("withdraw_id",withdrawId);
        return withdrawMapper.delete(queryWrapper) == 1;
    }


}
